SQL in 24 Hours, Sams Teach Yourself by Stephens Ryan & Jones Arie D. & Plew Ron

SQL in 24 Hours, Sams Teach Yourself by Stephens Ryan & Jones Arie D. & Plew Ron

Author:Stephens, Ryan & Jones, Arie D. & Plew, Ron [Stephens, Ryan]
Language: eng
Format: azw3
Publisher: Pearson Education
Published: 2015-12-10T16:00:00+00:00


Subqueries with the SELECT Statement

Subqueries are most frequently used with the SELECT statement; although you can use them within a data manipulation statement as well. The subquery when employed with the SELECT statement retrieves data for the main query to use.

The basic syntax follows:

Click here to view code image

SELECT COLUMN_NAME [, COLUMN_NAME ]

FROM TABLE1 [, TABLE2 ]

WHERE COLUMN_NAME OPERATOR

(SELECT COLUMN_NAME [, COLUMN_NAME ]

FROM TABLE1 [, TABLE2 ]

[ WHERE ])

Following is an example:

Click here to view code image

SELECT E.EMPLOYEEID,E.LASTNAME,

A.AIRPORTNAME, E.SALARY

FROM EMPLOYEES E

INNER JOIN AIRPORTS A

ON E.AIRPORTID = A.AIRPORTID

WHERE E.SALARY=

( SELECT SALARY

FROM EMPLOYEES

WHERE EMPLOYEEID=3908);

The preceding SQL statement returns the employee identification, last name, and salary for all employees who have a salary equal to that of the employee with the identification 3908. In this case, you do not necessarily know (or care) what the exact pay rate is for this particular employee; you care only about the pay rate for the purpose of getting a list of employees who bring home pay equal to the employee specified in the subquery.



Download



Copyright Disclaimer:
This site does not store any files on its server. We only index and link to content provided by other sites. Please contact the content providers to delete copyright contents if any and email us, we'll remove relevant links or contents immediately.